<script setup lang="ts">
import { ref } from 'vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '../../services/home'
import { BannerItem, CategoryItem, HotItem } from '../../types/home'
import { onMounted } from 'vue'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import { XtxGuessInstance } from '../../types/component'
// 轮播图列表
const bannerList = ref<BannerItem[]>([])
// 获取轮播图列表
const getBannerList = async () => {
  const res = await getHomeBannerAPI(1)
  bannerList.value = res.result
}
// 获取分类列表
const categoryList = ref<CategoryItem[]>([])
const getCategoryList = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}
// 获取热门推荐列表
const hotList = ref<HotItem[]>([])
const getHotList = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}
const guessRef = ref<XtxGuessInstance>()
const onScrolltolower = () => {
  guessRef.value?.getMore()
}
//下拉刷新
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  isTriggered.value = true
  guessRef.value?.resetData()
  await Promise.all([getBannerList(), getCategoryList(), getHotList()])
  isTriggered.value = false
  guessRef.value?.getMore()
}
onMounted(() => {
  getBannerList()
  getCategoryList()
  getHotList()
})
</script>

<template>
  <scroll-view
    class="scroll-view"
    scroll-y
    @scrolltoupper="onRefresherrefresh"
    @scrolltolower="onScrolltolower"
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
  >
    <!-- 自定义导航栏 -->
    <CustomNavbar />
    <!-- 轮播图 -->
    <XtxSwiper :list="bannerList" />
    <!-- 分类面板 -->
    <CategoryPanel :list="categoryList" />
    <!-- 热门推荐 -->
    <HotPanel :list="hotList" />

    <!-- 猜你喜欢 -->
    <XtxGuess ref="guessRef" />
  </scroll-view>
</template>

<style lang="scss">
//
.page {
  background-color: #f7f7f7;
  height: 100vh;
}
.scroll-view {
  height: calc(100vh - 100rpx);
}
</style>
